﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Container : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string[] name = { "张三", "李四" };
            string[] number = { "20180202782", "20180202783" };
            for (int i = 0; i < name.Length; i++)
            {
                TableRow row = new TableRow();
                TableCell cellName = new TableCell();
                TableCell cellNumber = new TableCell();
                cellName.Text = name[i];
                cellNumber.Text = number[i];
                //动态创建一个TextBox控件
                TextBox tb = new TextBox();
                TableCell cellGrade = new TableCell();
                //Controls：子控件
                cellGrade.Controls.Add(tb);
                //把列添加到行里面
                row.Cells.Add(cellName);
                row.Cells.Add(cellNumber);
                row.Cells.Add(cellGrade);
                //把行添加到表格里面
                Table1.Rows.Add(row);
            }

            TableRow rowFoot = new TableRow();
            //TableFooter:页脚的设置
            rowFoot.TableSection = TableRowSection.TableFooter;
            TableCell cellStat = new TableCell();
            //ColumnSpan：合并单元格
            cellStat.ColumnSpan = 2;
            cellStat.Text = "统计";
            //设置水平对齐方式为向右
            cellStat.HorizontalAlign = HorizontalAlign.Right;
            TableCell cellTotal = new TableCell();
            cellTotal.Text = "共计" + name.Count() + "条信息";
            //把列添加到行里面
            rowFoot.Cells.Add(cellStat);
            rowFoot.Cells.Add(cellTotal);
            //把行添加到表格里面
            Table1.Rows.Add(rowFoot);

            //开始的时候，只有第一个按钮可以看见，第二个按钮隐藏
            Panel1.Visible = true;
            Panel2.Visible = false;
        }

        //一个包含动态生成控件的页面需要往返处理，动态生成控件的代码必须放在Page_Load()方法中
        //动态生成控件Button
        Button btn = new Button();
        btn.ID = "BtnSubmit";
        btn.Text = "提交";
        //动态生成的控件不能在设计时（属性界面）直接绑定方法代码，需手工注册（事件）
        btn.Click += BtnSubmit_Click;
        PlaceHolder1.Controls.Add(btn);
        TextBox _tb = new TextBox();
        _tb.ID = "txtInput";
        PlaceHolder1.Controls.Add(_tb);
    }

    protected void BtnSubmit_Click(object sender, EventArgs e)
    {
        Label1.Text = "动态添加按钮事件响应";

    }

    //点击第一个按钮的时候，显示第二个按钮，第一个按钮隐藏
    protected void Button1_Click(object sender, EventArgs e)
    {
        Panel1.Visible = false;
        Panel2.Visible = true;
    }
    //点击第二个按钮的时候，显示文本之和，第一个按钮和第二个按钮都隐藏
    protected void Button2_Click(object sender, EventArgs e)
    {
        Panel1.Visible = false;
        Panel2.Visible = false;
        Label1.Text = Literal1.Text + Literal2.Text;
    }

    protected void ButtonTest_Click(object sender, EventArgs e)
    {
        //在获取动态生成的控件中的输入信息时，需要使用FindControl()方法先找到控件
        //as：
        TextBox _tb = PlaceHolder1.FindControl("txtInput") as TextBox;
        Label1.Text = _tb.Text;
    }

    //点击视图一的LinkButton,显示View1
    protected void LbtView1_Click(object sender, EventArgs e)
    {
        MultiView1.SetActiveView(View1);
    }
    //点击视图二的LinkButton,显示View2
    protected void LbtView2_Click(object sender, EventArgs e)
    {
        MultiView1.SetActiveView(View2);
    }
}